
import httputil.httpclientauth.RequestKerberosUrlUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by root on 20180118.
 * Update date:
 * <p>
 * Time: 11:34 AM
 * Project: test-java
 * Package: com.test.java.Thread
 * Describe :
 * <p>
 * Result of Test: test ok,test error
 * Command:
 * <p>
 * <p>
 * Email:  jifei.yang@ngaa.com.cn
 * Status：Using online
 * Attention：
 */
public class MonitSparkJobsHttpClient {
    private static Logger logger= LoggerFactory.getLogger(MonitSparkJobsHttpClient.class);

    public static void main(String[] args) {
        String keberosuser ="bigdata-app-ecpproddscat-ecpproddscat@ECOM.TLRG.COM";
        String keytab="D:\\work\\code\\mysuitecase\\conf\\clusters\\Fornax\\hbase.keytab";
        String krb5Location="D:\\work\\code\\mysuitecase\\conf\\clusters\\Fornax\\krb5.ini";


        try{
            RequestKerberosUrlUtils restTest = new RequestKerberosUrlUtils(keberosuser,keytab,krb5Location, false);

            // refer to https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Open_and_Read_a_File
            // list dir
            String url_liststatus="http://c286ghr.ecom.bigdata.int.thomsonreuters.com:8088/ws/v1/cluster/apps?";
            // location
            String url_get_block_locations="http://activeNamenodeHost:50070/webhdfs/v1/user/cdnportal/data/release/world.txt?op=get_block_locations";


            /*
             * 由于GET请求的参数都是拼装在URL地址后方，所以我们要构建一个URL，带参数
             */
            URIBuilder uriBuilder = new URIBuilder(url_liststatus);

            /** 第二种添加参数的形式 */
            List<NameValuePair> list = new LinkedList<>();
            BasicNameValuePair param1 = new BasicNameValuePair("queue", "root.ecpproddscat");
            BasicNameValuePair param2 = new BasicNameValuePair("user", "bigdata-app-ecpproddscat-ecpproddscat");
            BasicNameValuePair param3 = new BasicNameValuePair("states ", "RUNNING");
            BasicNameValuePair param4 = new BasicNameValuePair("applicationTags", "searchstreaming");
            list.add(param1);
            list.add(param2);
            list.add(param3);
            list.add(param4);
            uriBuilder.setParameters(list);

            // 根据带参数的URI对象构建GET请求对象
            HttpGet httpGet = new HttpGet(uriBuilder.build());

            /*
             * 添加请求头信息
             */
            // 浏览器表示
            //httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
            // 传输的类型
            //httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");

            HttpResponse response = restTest.callRestRequest(httpGet,keberosuser);
//          HttpResponse response = restTest.callRestUrl(url_get_block_locations,user);

            InputStream is = response.getEntity().getContent();
            System.out.println("Status code " + response.getStatusLine().getStatusCode());
            System.out.println("message is :"+ Arrays.deepToString(response.getAllHeaders()));

            StringBuilder fullpath = new StringBuilder();
            String fileName = String.valueOf(System.currentTimeMillis());
            String dir = restTest.getClass().getClassLoader().getResource("").getPath();
            fullpath.append(dir );
            fullpath.append(fileName);
            fullpath.append("applications");
            fullpath.append(".json");
            FileOutputStream fos = new FileOutputStream( fullpath.toString() );

            fos.write(IOUtils.toByteArray(is) );
            fos.close();
            //System.out.println("string：\n"+new String(IOUtils.toByteArray(is), "UTF-8"));

        }catch (Exception exp){
            exp.printStackTrace();
        }

    }
}
